﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System.Collections.Generic;
using System.Linq;
using NJamb.NUnit;
using NJamb.Parser.SpecificationBuilders;
using NJamb.Parser.SubjectBuilders;
using NJamb.Specifications.Delta;
using NUnit.Framework;
using Stile.Patterns.SelfDescribingPredicates;
using Stile.Readability;
#endregion

namespace NJamb.Tests.Specifications.Delta.Enumerable
{
    [TestFixture]
    public class EnumerableDeltaAcceptanceTests : EvaluatingSampleFixture
    {
        [Test]
        public void AndLaterHasCount()
        {
            // arrange
            IPreconditionEvaluation preconditionEvaluation =
                Specify.For(() => _sample).ThatTheSequence(x => x.IntEnumerable).Satisfies(Expect<IEnumerable<int>>.Whatever) //
                    .AndLater.Has.CountOf(0).EvaluatePrecondition();

            // act
            IEvaluation before = preconditionEvaluation.Evaluation;
            IEvaluation after = preconditionEvaluation.GetPostcondition().Evaluate();

            // assert
            Assert.That(before.Success, Is.True);
            Assert.That(after.Success, Is.True);
        }

        [Test]
        public void AndLaterIsNot()
        {
            // arrange
            IPreconditionEvaluation preconditionEvaluation =
                Specify.For(() => _sample).ThatTheSequence(x => x.IntEnumerable).Is.Empty //
                    .AndLater.Is.Not.Empty.EvaluatePrecondition();

            // act

            IEvaluation before = preconditionEvaluation.Evaluation;
            _sample.AddToEnumerable(3);
            IEvaluation after = preconditionEvaluation.GetPostcondition().Evaluate();

            // assert
            Assert.That(before.Success, Is.True);
            Assert.That(after.Success, Is.True);
        }

        [Test]
        public void Contains()
        {
            // arrange
            _sample.AddToEnumerable(9);
            IDeltaSpecification deltaSpecification =
                Specify.For(() => _sample).ThatTheSequence(x => x.IntEnumerable).Contains(9).AndLater.Contains(7);
            IPreconditionEvaluation preconditionEvaluation = deltaSpecification.EvaluatePrecondition();

            // act
            IEvaluation before = preconditionEvaluation.Evaluation;
            Assert.That(_sample.IntEnumerable.Any(x => x == 7), Is.False, "precondition");
            IEvaluation after = preconditionEvaluation.GetPostcondition().Evaluate();

            // assert
            Assert.That(before.Success, Is.True);
            AssertEvaluationFailed(after,
                "expected _sample.IntEnumerable would contain 7",
                " but was " + _sample.IntEnumerable.ToDebugString() + ".");
        }
    }
}
